
const stopKeys = function (e, parameter = { dot: true }) {
  /**
  * @param e : EVENT
  * @param parameter {Object}
  * Vue模板中调用：
  * @keydown = "stopKeys"
  * 禁用小数点：
  * @keydown = "stopKeys($event,{ dot: false })"
  */
  // # keyCodes:
  // 键盘上方的数字键(0 - 9)
  // [48,49,50,51,52,53,54,55,56,57]
  // 数字键盘数字键(0 - 9)
  // [96,97,98,99,100,101,102,103,104,105]
  // 小数点(.)
  // [110,190]
  // 其他功能键：
  // 退格键[8] && TAB键[9] && SHIFT键[16]

  const KEYBOARD_TOP = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
  const KEYBOARD_NUMERIC = [96, 97, 98, 99, 100, 101, 102, 103, 104, 105]
  const DOTS = parameter.dot ? [110, 190] : []
  const FUNCTION_KEYS = [8, 9, 16]
  const CODES = [
    ...KEYBOARD_TOP,
    ...KEYBOARD_NUMERIC,
    ...DOTS,
    ...FUNCTION_KEYS
  ]

  const currentCode = e.keyCode
  const passed = CODES.some(code => code === currentCode)

  if (!passed) {
    e.preventDefault()
  }
}


let lessNodesAppended;

const updateTheme = theme => {
  // Don't compile less in production!
  /* if (process.env.NODE_ENV === 'production') {
    return;
  } */
  // Determine if the component is remounted
  /**
   * 使用浏览器端less,修改全局less变量,以改变And Design主题色调
   * 参考[ant-design-pro-vue](https://github.com/sendya/ant-design-pro-vue)
   */
  if (!theme) {
    return
  }
  // #363842
  //const hideMessage = message.loading('正在编译主题！', 2)
  console.log('主题颜色：', theme);

  const primaryColor = theme == 'white' ? {
    '@primary-color': '#1da57a',
    '@body-background': '#fff',
    '@border-radius-base': '2px',
    '@input-bg': 'transparent'
  } : {
      '@primary-color': '#666',
      '@body-background': '#363842',
      '@border-radius-base': '2px',
      '@input-bg': 'transparent',

      '@component-background': '#464852',
      '@text-color': '#d7d7d7',
      '@text-color-secondary': '#d7d7d7',
      '@primary-1': '#626b76',
      '@radio-button-bg': 'transparent',
      '@heading-color': '#f1f1f1',
      '@table-header-bg': 'transparent',
      '@table-selected-row-bg': '#626b76',
      '@checkbox-check-color': 'transparent',
      '@checkbox-color': '#1da57a'
    }

  function buildIt() {
    if (!window.less) return;
    setTimeout(() => { window.less.modifyVars(primaryColor) }, 200)
  }

  if (!lessNodesAppended) {
    // insert less.js and color.less
    const lessStyleNode = document.createElement('link');
    const lessConfigNode = document.createElement('script');
    const lessScriptNode = document.createElement('script');
    lessStyleNode.setAttribute('rel', 'stylesheet/less');
    lessStyleNode.setAttribute('href', '/color.less');
    lessConfigNode.innerHTML = `
      window.less = {
        async: true,
        env: 'production',
        javascriptEnabled: true
      };
    `
    lessScriptNode.src = 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js';
    lessScriptNode.async = true;
    lessScriptNode.onload = () => {
      buildIt();
      lessScriptNode.onload = null
    }
    document.body.appendChild(lessStyleNode);
    document.body.appendChild(lessConfigNode);
    document.body.appendChild(lessScriptNode);
    lessNodesAppended = true
  } else {
    buildIt()
  }
}

export {
  stopKeys,
  updateTheme
}
